
package org.example.baseinfo.service;

import org.example.baseinfo.common.page.HxPage;
import org.example.baseinfo.common.page.HxPageRequest;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;

public interface BaseService {

    /**
     * 实体转化为VO
     */
    default <V, E> List<V> toVoList(List<E> entityList, Class<V> vClass) {
        List<V> vList = new ArrayList<>();
        if (entityList == null || entityList.isEmpty()) {
            return vList;
        }
        for (E entity : entityList) {
            V vo;
            try {
                vo = vClass.newInstance();
            } catch (InstantiationException | IllegalAccessException e) {
                continue;
            }
            BeanUtils.copyProperties(entity, vo);
            vList.add(vo);
        }
        return vList;
    }

    /**
     * 空分页结果
     *
     */
    default <T> HxPage<T> emptyPage(HxPageRequest<?> pageRequest) {
        HxPage<T> hxPage = new HxPage<>();
        hxPage.setTotalPages(0);
        hxPage.setPageSize(pageRequest.getPageSize());
        hxPage.setPageNum(pageRequest.getPageNum());
        hxPage.setContent(new ArrayList<>());
        return hxPage;
    }

}
